package com.lx.huimail.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimail.MyApplication;
import com.lx.huimail.R;
import com.lx.huimail.configs.Config;
import com.lx.huimail.utils.TimerCount;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import okhttp3.Call;

public class ForgetPasswordActivity extends Activity implements OnClickListener{

	private LinearLayout fanhui;
	private EditText mobile;
	private EditText yanzhengma;
	private TextView huoqu;
	private Button next;
	private String txt;
	private String hello;
	private String codeByService;
	public static Activity instance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_forget_password);
		instance=this;
		hello=getIntent().getStringExtra("hello");
		initView();
	}

	private void initView() {
		mobile=(EditText)findViewById(R.id.mobile);
		yanzhengma=(EditText)findViewById(R.id.yanzhengma);
		huoqu=(TextView)findViewById(R.id.huoqu);
		fanhui=(LinearLayout)findViewById(R.id.fanhui);
		fanhui.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		next=(Button)findViewById(R.id.next);
		huoqu.setOnClickListener(this);
		next.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.huoqu:
				huoqu();
				break;

			case R.id.next:

				if (mobile.getText().toString().equals("")){
					Toast.makeText(ForgetPasswordActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if (yanzhengma.getText().toString().equals("")){
					Toast.makeText(ForgetPasswordActivity.this, "请输入验证码", Toast.LENGTH_SHORT).show();
					return;
				}
				if(!codeByService.equals("\""+yanzhengma.getText().toString()+"\"")){
					Toast.makeText(this,"验证码填写不正确",Toast.LENGTH_SHORT).show();
					return;
				}
				Intent intent=new Intent(ForgetPasswordActivity.this,ForgetPasswordnextActivity.class);
				intent.putExtra("phone", mobile.getText().toString());
				intent.putExtra("xiaoyanma",yanzhengma.getText().toString());
				startActivity(intent);

				break;
		}

	}

	private void huoqu() {
		txt = mobile.getText().toString();
		if(hello.equals("1")){
			if (txt.equals("")) {
				Toast.makeText(this, "请输入手机号！", Toast.LENGTH_SHORT).show();
				return;
			}if(!isMobileNO(txt)){
				Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
			}else{

				OkHttpUtils
						.post()
						.url(Config.url+"login/checkPhoneExist.do")
						.addParams("phone",txt)
						.addParams("mobile","true")
						.build()
						.execute(new StringCallback() {
							@Override
							public void onError(Call call, Exception e, int id) {
								System.out.print("失败=="+e.getMessage());
							}

							@Override
							public void onResponse(String response, int id) {
								if (response.equals("\"false\"")){
									Toast.makeText(ForgetPasswordActivity.this, "手机号错误",Toast.LENGTH_LONG).show();
								}else if (response.equals("\"true\"")){
									TimerCount timer = new TimerCount(60000, 1000, huoqu);
									timer.start();

									HashMap<String, String> map = new HashMap<>();
									map.put("phone",txt);
									Gson gson=new Gson();
									String str=gson.toJson(map).toString();
									OkHttpUtils
											.post()
											.url(Config.url+"login/appregistcode.do")
											.addParams("data",str)
											.build()
											.execute(new StringCallback() {
												@Override
												public void onError(Call call, Exception e, int id) {

												}

												@Override
												public void onResponse(String response, int id) {
													System.out.print("验证码=="+response);
													codeByService=response;
												}
											});
								}


							}
						});


			}
		}
		if (hello.equals("2")){
			if (txt.equals("")) {
				Toast.makeText(this, "请输入手机号！", Toast.LENGTH_SHORT).show();
				return;
			}if(!isMobileNO(txt)){
				Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
			}else {
				OkHttpUtils
						.post()
						.url(Config.url + "login/appcheckPhone.do")
						.addParams("phone",txt)
						.build()
						.execute(new StringCallback() {
							@Override
							public void onError(Call call, Exception e, int id) {
								System.out.print("失败==" + e.getMessage());
							}

							@Override
							public void onResponse(String response, int id) {
								System.out.print("号码检查==" + response);
								if (response.equals("\"false\"")) {
									Toast.makeText(ForgetPasswordActivity.this, "手机号未注册", Toast.LENGTH_LONG).show();
								} else if (response.equals("\"true\"")) {
									TimerCount timer = new TimerCount(60000, 1000, huoqu);
									timer.start();

									HashMap<String, String> map = new HashMap<>();
									map.put("phone", txt);
									Gson gson = new Gson();
									String str = gson.toJson(map).toString();
									OkHttpUtils
											.post()
											.url(Config.url + "login/appregistcode.do")
											.addParams("data", str)
											.build()
											.execute(new StringCallback() {
												@Override
												public void onError(Call call, Exception e, int id) {

												}

												@Override
												public void onResponse(String response, int id) {
												codeByService=response;
												}
											});
								}

							}
						});

			}
		}
	}
	private boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("[1][34578]\\d{9}" );

		Matcher m = p.matcher(mobiles);

		return m.matches();
	}

}
